<?php


  class ZiyuanAction extends CommonAction{
  
    public function index(){
	
	  $this->user=M('dailiuser')->where('id="%s"',session('did'))->find();
	  
	  
	  //dump($this->user);
	  
	  $this->sys=M('system')->where('id=1')->find();
	  
	  //dump($this->user['keyword']);
	  
	  $this->display();
	
	}
	
	
	
	public function my(){
	
	  if(isset($_GET['keywords'])){
	    
		$keywords=$_GET['keywords'];
		
		$where['title']=array('LIKE',"%$keywords%");
	  
	    $this->keywords=$keywords;
	  
	  }
	
	  
	   $this->user=M('dailiuser')->where('id="%s"',session('did'))->find();
	  
	  
	  //dump($this->user);
	  
	  $this->sys=M('system')->where('id=1')->find();
	
	
	  $where['dailiid']=array('eq',session('did'));
	 
	  $re=$this->page('pianku','10','id desc',$where);
	
	  $this->re=$re;
	  
	  
	  $this->yuangong=M('yuangong')->where('uid="%s"',session('did'))->select();
	  
	
	  $this->display();
	
	}
	
	
	public function shanchu(){
	
	
	    if($_POST['moshi']==1){
	
	  
	       foreach($_POST['ids'] as $v){
			
			  $re=M('pianku')->where('id="%s"',$v)->delete();
			  
			}
		    
			if($re){
			
			  $this->success('删除成功');exit();
			
			
			}else{
			
			  $this->error('删除失败');
			
			
			}
		
	   }else{
	   
	   
	   
	   
	      foreach($_POST['ids'] as $v){
	
		  $arr['pid']=$v;
		  
		  $arr['uid']=session('did');
		  
		  $arr['time']=time();
		  
		  $arr['yingli']=0;
		  
		 
		  
		  //生成短连接
		  $sys=M('system')->where('id=1')->field('tiaozhuanlianjie')->find();
		  
		  $re=M('mypianku')->add($arr);
		  
		  
		  $longurl=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index/uid/".session('did')."/id/".$v."/pid/".$re."/";
		  
		  $data['url']=$this->duanlianjie($longurl);
		  
		  M('mypianku')->where('id="%s"',$re)->save($data);
	  
	      } 
	  
	   
	   
	    
	          if($re){
	  
				$this->success('批量生成链接成功');exit();
			  }else{
			  
				$this->error('生成失败');
			  }
	   
	   
	   
	   
	   
	   
	   
	   
	   }
	
	
	
	
	}
	
	
	public function add(){
	
	
	  foreach($_POST['ids'] as $v){
	
		  $arr['pid']=$v;
		  
		  $arr['uid']=session('did');
		  
		  $arr['time']=time();
		  
		  $arr['yingli']=0;
		  
		 
		  
		  //生成短连接
		  $sys=M('system')->where('id=1')->field('tiaozhuanlianjie')->find();
		  
		  $re=M('mypianku')->add($arr);
		  
		  
		  $longurl=$sys['tiaozhuanlianjie']."/index.php/tiaozhuan/index/uid/".session('did')."/id/".$_POST['id']."/pid/".$re."/";
		  
		  $data['url']=$this->duanlianjie($longurl);
		  
		  M('mypianku')->where('id="%s"',$re)->save($data);
	  
	 } 
	  
	  if($re){
	  
	    $ajax=1;
	  }else{
	  
	    $ajax=2;
	  }
	  
	  $this->ajaxReturn($ajax);
	
	}
	
	
	
	public function del(){
	
	  $re=M('pianku')->where('id="%s"',$_POST['id'])->find();
	  
	  if($re['dailiid']!=session('did')){
	  
	    $ajax=3;
		
		$this->ajaxReturn($ajax);
	  
	  }
	  
	
	  $this->ajaxReturn((M('pianku')->where('id="%s"',$_POST['id'])->delete())?1:2);
	
	
	
	}
	
  
  
  }